package com.paomi.client.activity.classify;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.classify.adapter.ClassifyListAdapter;
import com.paomi.client.activity.classify.model.SearchGetBeaan;
import com.paomi.client.activity.newsDetail.model.NewsListJson;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.fragment.first.model.TicketJson;
import com.paomi.client.utils.UIUtils;
import com.paomi.client.view.SearchPopupMenu;
import com.paomi.client.widget.RxToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索分类列表的展示
 */
public class SearchClassifyActivity extends BaseMvpActivity<SearchClassifyMvpView, SearchClassifyPresenter> implements SearchClassifyMvpView {

    @BindView(R.id.tv_search_icon)
    TextView tvSearchIcon;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.tv_search_clear)
    TextView tvSearchClear;
    @BindView(R.id.classify_all_text)
    TextView classifyAllText;
    @BindView(R.id.classify_all_image)
    ImageView classifyAllImage;
    @BindView(R.id.classify_all)
    LinearLayout classifyAll;
    @BindView(R.id.classify_hot_text)
    TextView classifyHotText;
    @BindView(R.id.classify_hot_image)
    ImageView classifyHotImage;
    @BindView(R.id.classify_hot)
    LinearLayout classifyHot;
    @BindView(R.id.recycler_search)
    RecyclerView recyclerSearch;
    @BindView(R.id.ll_none)
    LinearLayout llNone;

    private SearchPopupMenu searchPopupMenu=null;
    private List<SearchGetBeaan> stringList=new ArrayList<>();//下拉框的数据
    private List<SearchGetBeaan> classifyList=new ArrayList<>();//搜索列表的数据
    private String searchContent="";
    private int pageNum=1;//页码
    private  ClassifyListAdapter classifyListAdapter;
    private List<TicketJson> ticketJsonS=new ArrayList<>();//演出信息
    private List<NewsListJson> listJsons=new ArrayList<>();//资讯数据信息
    private SearchGetBeaan getBean=null;//全部信息列表的选择
    private SearchGetBeaan getSearch=null;//热度，时间选择

    @NonNull
    @Override
    public SearchClassifyPresenter createPresenter() {
        return new SearchClassifyPresenter();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.title_classify);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
        button_goback.setTypeface(iconfont);
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_classiy;
    }

    @Override
    protected void initEvent() {
        tvSearchIcon.setTypeface(iconfont);
        tvSearchClear.setTypeface(iconfont);
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((actionId == 6) || (actionId == 2) || ((event != null) && (event.getAction() == 0) && (event.getKeyCode() == 66))) {
                    if (etSearch.getText().toString() == null || etSearch.getText().toString().length() < 1) {
                        RxToast.info(getResources().getString(R.string.search_null));
                        return true;
                    }
                    searchContent=etSearch.getText().toString();
                    //请求演出和资讯的接口
                    classifyList.clear();
                    getPresenter().getShowList(searchContent,pageNum,"0");
                    getPresenter().setInfoData(pageNum,searchContent,"0");
                    return true;
                }
                return false;
            }
        });
        recyclerSearch.setLayoutManager(UIUtils.getRecyclerViewManager(false,this));
       classifyListAdapter=new ClassifyListAdapter(this,iconfont);
        recyclerSearch.setAdapter(classifyListAdapter);
//        classifyListAdapter.setData(classifyList,null);
        if(classifyList.size()>0){
            llNone.setVisibility(View.GONE);
        }else{
            llNone.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.tv_search_clear, R.id.classify_all, R.id.classify_hot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_search_clear://清空搜索内容
                etSearch.setText("");
                break;
            case R.id.classify_all://全部分类
                stringList.clear();
                SearchGetBeaan searchGetBeaan=new SearchGetBeaan();
                searchGetBeaan.setName(getResources().getString(R.string.classify_all));
                searchGetBeaan.setText(getResources().getString(R.string.list_all));
                searchGetBeaan.setId(0);
                stringList.add(searchGetBeaan);
                SearchGetBeaan searchGetBeaan1=new SearchGetBeaan();
                searchGetBeaan1.setName(getResources().getString(R.string.classify_show));
                searchGetBeaan1.setText(getResources().getString(R.string.list_show));
                searchGetBeaan1.setId(1);
                stringList.add(searchGetBeaan1);
                SearchGetBeaan searchGetBeaan2=new SearchGetBeaan();
                searchGetBeaan2.setName(getResources().getString(R.string.classify_shop));
                searchGetBeaan2.setText(getResources().getString(R.string.list_shop));
                searchGetBeaan2.setId(2);
                stringList.add(searchGetBeaan2);
                SearchGetBeaan searchGetBeaan3=new SearchGetBeaan();
                searchGetBeaan3.setName(getResources().getString(R.string.classify_news));
                searchGetBeaan3.setText(getResources().getString(R.string.list_mess));
                searchGetBeaan3.setId(3);
                stringList.add(searchGetBeaan3);
                searchPopupMenu=new SearchPopupMenu(this,stringList);
                searchPopupMenu.showLocation(R.id.classify_all);
                searchPopupMenu.setOnClassifyClickListener(new SearchPopupMenu.OnClassifyClickListener() {
                    @Override
                    public void onGetDataClick(View view, int pos) {
                        String get_id="0";
                        getBean=stringList.get(pos);
                        if(getSearch!=null){
                            if(getSearch.getName().endsWith(getResources().getString(R.string.hot))){
                                get_id="1";
                            }else{
                                get_id="0";
                            }
                        }
                        classifyAllText.setText(getBean.getName());
                        if(getBean.getName().equals(getResources().getString(R.string.classify_all))){
                            classifyList.clear();
                            recyclerSearch.setVisibility(View.VISIBLE);
                            getPresenter().getShowList(searchContent,pageNum,get_id);
                            getPresenter().setInfoData(pageNum,searchContent,get_id);
                        }else if(getBean.getName().equals(getResources().getString(R.string.classify_show))){//演出
                            classifyList.clear();
                            recyclerSearch.setVisibility(View.VISIBLE);
                            getPresenter().getShowList(searchContent,pageNum,get_id);
                        }else if(getBean.getName().equals(getResources().getString(R.string.classify_news))){//资讯
                            classifyList.clear();
                            recyclerSearch.setVisibility(View.VISIBLE);
                            getPresenter().setInfoData(pageNum,searchContent,get_id);
                        }else if(getBean.getName().equals(getResources().getString(R.string.classify_shop))){//周边
                            classifyList.clear();
                            llNone.setVisibility(View.VISIBLE);
                            recyclerSearch.setVisibility(View.GONE);
                        }
                    }
                });
                break;
            case R.id.classify_hot://热度
                stringList.clear();
                SearchGetBeaan searchGetBeaan4=new SearchGetBeaan();
                searchGetBeaan4.setName(getResources().getString(R.string.hot));
                searchGetBeaan4.setText(getResources().getString(R.string.classify_choose_hot));
                searchGetBeaan4.setId(0);
                SearchGetBeaan searchGetBeaan5=new SearchGetBeaan();
                searchGetBeaan5.setName(getResources().getString(R.string.time));
                searchGetBeaan5.setText(getResources().getString(R.string.classify_choose_time));
                searchGetBeaan5.setId(0);
                stringList.add(searchGetBeaan5);
                stringList.add(searchGetBeaan4);
                searchPopupMenu=new SearchPopupMenu(this,stringList);
                searchPopupMenu.showLocation(R.id.classify_hot);
                searchPopupMenu.setOnClassifyClickListener(new SearchPopupMenu.OnClassifyClickListener() {
                    @Override
                    public void onGetDataClick(View view, int pos) {
                        String id_get="";
                        getSearch=stringList.get(pos);
                        classifyHotText.setText(getSearch.getName());
                        if(getSearch.getName().endsWith(getResources().getString(R.string.hot))){
                            id_get="1";
                        }else{
                            id_get="0";
                        }
                        classifyList.clear();
                        if(getBean!=null){
                            if(getBean.getName().equals(getResources().getString(R.string.classify_all))){
                                classifyList.clear();
                                recyclerSearch.setVisibility(View.VISIBLE);
                                getPresenter().getShowList(searchContent,pageNum,id_get);
                                getPresenter().setInfoData(pageNum,searchContent,id_get);
                            }else if(getBean.getName().equals(getResources().getString(R.string.classify_show))){//演出
                                classifyList.clear();
                                recyclerSearch.setVisibility(View.VISIBLE);
                                getPresenter().getShowList(searchContent,pageNum,id_get);
                            }else if(getBean.getName().equals(getResources().getString(R.string.classify_news))){//资讯
                                classifyList.clear();
                                recyclerSearch.setVisibility(View.VISIBLE);
                                getPresenter().setInfoData(pageNum,searchContent,id_get);
                            }else if(getBean.getName().equals(getResources().getString(R.string.classify_shop))){//周边
                                classifyList.clear();
                                llNone.setVisibility(View.VISIBLE);
                                recyclerSearch.setVisibility(View.GONE);
                            }
                        }else{
                            getPresenter().setInfoData(pageNum,searchContent,id_get);
                            getPresenter().getShowList(searchContent,pageNum,id_get);
                        }
                    }
                });
                break;
        }
    }

    /**
     * 显示演出的数据
     * @param ticketJsonList
     */
    @Override
    public void setShowData(List<TicketJson> ticketJsonList) {
        if(ticketJsonList.size()>0){
            llNone.setVisibility(View.GONE);
            ticketJsonS.clear();
            ticketJsonS.addAll(ticketJsonList);
            SearchGetBeaan searchGetBeaan=new SearchGetBeaan();
            searchGetBeaan.setName(getResources().getString(R.string.classify_show));
            classifyList.add(searchGetBeaan);
        }
        classifyListAdapter.setData(classifyList,ticketJsonS,listJsons);
        classifyListAdapter.notifyDataSetChanged();
    }

    /**
     * 显示资讯的数据
     * @param result
     */
    @Override
    public void getInfoData(List<NewsListJson> result) {
        if(result.size()>0){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    llNone.setVisibility(View.GONE);
                    SearchGetBeaan searchGetBeaan=new SearchGetBeaan();
                    searchGetBeaan.setName(getResources().getString(R.string.classify_news));
                    classifyList.add(searchGetBeaan);
                    listJsons.clear();
                    listJsons.addAll(result);
                    classifyListAdapter.setData(classifyList,ticketJsonS,listJsons);
                    classifyListAdapter.notifyDataSetChanged();
                }
            },1000);
        }
    }
}
